package com.lifeng.config;

import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author:lifeng
 * @date:2020/7/25 16:50
 */
@Configuration
@EnableRabbit
public class RabbitMQConfig {
    /**
     * 该类封装了对 RabbitMQ 的管理操作
     * 比如操作exchange queue bind等
     * 注意但是一般交换机 队列 等操作都在rabbit控制台操作,程序里不配置
     * */
    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(false);
        return rabbitAdmin;
    }
    @Bean
    public MessageConverter messageConverter(){
        Jackson2JsonMessageConverter jsonConverter = new Jackson2JsonMessageConverter();
        jsonConverter.setDefaultCharset("UTF-8");
        return jsonConverter;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        //设置消息转换
        rabbitTemplate.setMessageConverter(messageConverter());
        return rabbitTemplate;
    }

}
